var Trees = require('./libs/jurisdictions');

var randomstring = require("randomstring");

var mslibrary = require("ms-common-library");

module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var Jurisdictions = Trees(config);
    var router = function (req, res, next) {
        var ajaxTest = {
            error: 0
        };
        var Request = req.body;

        var Socket = config.Socket;

        if (Request["username"] != null && Request["password"] != null) {
            var ajaxTest = {
                error: 0
            };
            var LoadingDate = req.session.LoadingDate;
            if (LoadingDate && (new Date() - new Date(LoadingDate)) / 1000 < (req.session.LoginErrorlen - 3) * 5 * 60) {
                ajaxTest.error = 1001;
                ajaxTest.LoadingDate = parseInt(Math.abs((new Date() - new Date(LoadingDate)) / 1000 - ((req.session.LoginErrorlen - 3) * 5 * 60)));
            } else { 
                new source("M_2", Request, function (db) {
                    if (db) {
                        req.session.key = req.sessionID;
                        var Socket_id = randomstring.generate(32);
                        var setSession = function (db) {
                            var socketid = UserControl.Userid2Socketid(db.id);
                            if (socketid) {
                                Socket.SetClientMessage(socketid, {
                                    "key": "remote-login"
                                })
                            };
                            UserControl.setvalue(req.session.key, {
                                socket_id: Socket_id,
                                user_id: db.id,
                                roles_id: db.roles_id
                            });
                            UserControl.setUserSource(db.roles_id, db.Sources);
                            UserControl.SetSocket(req.session.key, Socket_id);

                            //Socket
                        }
                        var Writedb = function (db) {

                            var token = mslibrary.MD5(db.filename + '/' + db.id + "/" + config.id + "/" + config.version+ "/" + package.version);

                            req.token.set(token);

                            res.Json({
                                error: 0,
                                token: token,
                                socket_id: Socket_id
                            });
                            return
                            var etag_key = 'W/ms-' + token;
                            if (etag_key == req.getHeader('if-none-match')) {
                                req.notModified()
                                return
                            } else {
                                res.etag_key = etag_key;
                            }

                            Jurisdictions(db, function (_db) {
                                _db.socket_id = Socket_id;
                                _db.token = token;
                                 
                                res.Json(_db);
                            });

                        }
                        setSession(db)
                        Writedb(db)
                    } else {
                        req.session.LoginErrorlen++;
                        ajaxTest.error = 1;
                        if (req.session.LoginErrorlen > 3) {
                            ajaxTest.error = 1001;
                            req.session.LoadingDate = new Date();
                            var LoadingDate = req.session.LoadingDate;
                            ajaxTest.LoadingDate = parseInt(Math.abs((new Date() - new Date(LoadingDate)) / 1000 - ((req.session.LoginErrorlen - 3) * 5 * 60)));
                        }
                        res.Json(ajaxTest);
                    }
                });
            }
        } else {
            ajaxTest.error = 10001;
            res.Json(ajaxTest);
        }
    }
    return router;

}